using MyWeb.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using MyWeb.Model;

namespace MyWeb.Pages;

/// <summary>
/// 
/// </summary>
public class PizzaListModel : PageModel
{
    /// <summary>
    /// 
    /// </summary>
    private readonly PizzaService _service;
    /// <summary>
    /// 
    /// </summary>
    /// <value></value>
    public List<Pizza> PizzaList { get;set; } = default!;

    /// <summary>
    /// 
    /// </summary>
    /// <value></value>
    [BindProperty]
    public Pizza NewPizza { get; set; } = default!;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="service"></param>
    public PizzaListModel(PizzaService service)
    {
        _service = service;
    }

    /// <summary>
    /// 
    /// </summary>
    public void OnGet()
    {
        PizzaList = _service.GetPizzas();
    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    public IActionResult OnPost()
    {
        if (!ModelState.IsValid || NewPizza == null)
        {
            return Page();
        }

        _service.AddPizza(NewPizza);

        return RedirectToAction("Get");
    }
}

